﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;

namespace CoreEx.Database.Client.Tests
{
    [Implements(typeof(IProfiler<IDbTransaction>),LifecycleType.Singleton)]
    public class SampleTransactionProfiler : IProfiler<IDbTransaction>        
    {
        private Stopwatch _stopwatch;

        public long TimeElapsed { get; set; }

        public void BeforeInvoke(IInvocationInfo info)
        {
            
        }

        public void AfterInvoke(IInvocationInfo info, object returnValue)
        {
            if (info.TargetMethod.Name == "BeginTransaction")
            {
                Console.WriteLine("BeginTransaction");
                _stopwatch = new Stopwatch();
                _stopwatch.Start();
            }
            if (info.TargetMethod.Name == "Commit" || info.TargetMethod.Name == "Rollback")
            {
                Console.WriteLine("{0}Transaction", info.TargetMethod.Name);
                _stopwatch.Stop();
                TimeElapsed = _stopwatch.ElapsedMilliseconds;                
            }
        }
    }
}
